<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>物理服务器管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script src="/js/xkube.js?v=1" charset="utf-8"></script>
    <script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>

<style type="text/css">
  {# 设置table每一行的height #}
  .layui-table-cell {
   height: 50px;
   line-height: 50px;
  }
</style>

</head>
<body>
<blockquote class="layui-elem-quote layui-text">
    注:集群ID有写入到cookie,如有更改集群id，需要清理浏览器缓存或清理cookies
</blockquote>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-sm data-count-edit" lay-event="edit">编辑</a>
            <!-- <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="monitor">监控</a> -->
            <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="setDefaultCluster">设为常用集群</a>
            <a href="javascript:;" layuimini-content-href="page/wiki/list.html?xcolumn={{d.cluster_id}}" data-title="{{ d.cluster_id }}文章" class="layui-btn layui-btn-normal layui-btn-sm" lay-event="wiki">wiki</a>
            <a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
</body>
<script type="text/html" id="statusTpl">
  {{# if (d.status == '1') { }}
     <span style="color:#218868">运行中</span>
	{{# } else { }}
    <span style="color:#7A8B8B">下线</span>
{{# } }}
</script>	
<script type="text/html" id="DefaultClusterTpl">
  {{# if (d.cluster_id == getCookie("clusterId")) { }}
     <span style="color:#FF5722">常用集群</span>
	{{# } else { }}
    <span></span>
{{# } }}
</script>	
<script>

    layui.use(['form', 'table','miniTab'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniTab = layui.miniTab;
            miniTab.listen();

        table.render({
            elem: '#currentTableId',
            url: '/xkube/cluster/v1/List',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                //{type: "checkbox", width: 50},
                {field: 'id', width:70, title: 'ID', sort: true, hide:true},
                {field: 'cluster_id',width: 150, title: '集群id', sort: true},
                {field: 'cluster_name',title: '集群名称'},
                {field: 'idc_name', width:100, title: 'IDC'},
                {field: 'kube_version',title: '版本'},
                {field: 'lan_slbip', title: '内网SLB'},
                {field: 'wan_slbip', title: '公网SLB'},
                {field: 'status', title: '状态',templet: '#statusTpl', width: 100},
                {field: '', title: '默认集群',templet: '#DefaultClusterTpl', width: 100},
                {field: 'createtime', width:150, title: '创建时间', hide:true},
                {field: 'remarks', width:100, title: '备注'},
                {title: '操作', minWidth: 350, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [25, 50, 100],
            limit: 25,
            page: true,
            done: function (res, curr, count) {
              	var clusterId = getQueryString("clusterId");
              	if (clusterId == null) {
              		clusterId = getCookie("clusterId")
              		if (clusterId == "") {
          					if ( res.msg == "NoLogin" ) {
          						layer.msg('请先登陆',{icon:2});
          					}
          					if (count > 0) {
          						setCookie('clusterId',res.data[0].cluster_id,30);
                      window.parent.location.reload();
          					}else{
          						layer.msg('读取k8s集群信息失败，请先添加k8s配置',{icon:2});
          					}
                  }
                }   
            },
    				error: function(xhr, status, error) {
        				layer.msg('获取集群列表失败',{icon:2});
      			}
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
                data.field.pcg_ip = data.field.pcg_ip.replace(/^\s*|\s*$/g,""); //替换空格
                data.field.pcg_guest_name = data.field.pcg_guest_name.replace(/^\s*|\s*$/g,"");
            var result = JSON.stringify(data.field);
            //layer.alert(result, {
            //    title: '最终的搜索信息'
            //});

            //执行搜索重载
            table.reload('currentTableId', {
                //page: {
                //    curr: 1,
                //    limit:50
                //},
                method:'POST',
                url: '/xkube/cluster/v1/List',
                contentType: 'application/json',
                where: data.field,
                done: function(res, curr, count){
                  //console.log(res);
                  //this.where={};
                  //不清空，有问题：搜索条件会保留
                  //清空，  有问题：分页的时候没有搜索条件了,分页会受到影响
                  //https://copyfuture.com/blogs-details/20200729003153292ltjtf01kvp048gr
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '92%'],
                    content: '/page/xkube/clusterAdd.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        //table.on('checkbox(currentTableFilter)', function (obj) {
         //   console.log(obj)
        //});

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['50%', '92%'],
                    content: '/page/xkube/clusterEdit.html?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'setDefaultCluster') {
                setCookie('clusterId',data.cluster_id,30);
                layer.msg('设置成功');
                window.location.reload();                
                //console.log(data);               
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除'+data.cluster_id+'?', {icon: 3, title:'删除提示',yes: function(index){
                     var index2 = layer.load(0, {shade: false});
                     layer.msg('此处需运行1-2s左右');
                			 $.ajax({
                			    url: '/xkube/cluster/v1/Del?id='+data.id,
                			    type: "get",
                			    //data: data,
                			    //dataType: "json",
                			    success: function (resp) {
                            layer.close(index2);
                						//console.log(resp);
                						 if(resp.code == 0){
                							 layer.msg('删除成功', {icon: 1});
                							 //window.location.reload();
                							 obj.del();
                						 }else{
                							 layer.msg(resp.msg,{icon:2});
                						 }
                				  }
                		   });		  	  
                },
            		  cancel: function(index, layero){ 
            			    layer.close(index);
                      layer.close(index2);
            			    console.log("不操作");
            		  } 
        		    });              
            }
        });
    });
</script>

</html>